package atguigu.pUtils;

import cn.hutool.core.collection.CollectionUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ActItem {
    private String name;// 活动名称

    private int es;// 最早开始
    private int ls;// 最晚开始
    private int ef; // 最早结束
    private int lf;// 最晚结束

    private int tf; // 总浮动时间
    private int ff;// 自由浮动时间

    private int du;// 需要天数



    List<ActItem> next;
    List<ActItem> pre;

    public ActItem() {
    }

    public ActItem(String name, int du) {
        this.name = name;
        this.du = du;
    }

    public void addNext(ActItem actItem){
        if(this.next == null){
            this.next = new ArrayList<>();
        }
        next.add(actItem);
        actItem.addPre(this);

        if(this.pre == null){
            this.setEs(0);
            if(du > 0)
                this.setEf(du);
            else
                this.setEf(0);
        }else{
            // 紧前活动中的最早结束时间的最大值
            List<Integer> collect = getPre().stream().map(e -> e.getEf()).collect(Collectors.toList());
            Integer max = CollectionUtil.max(collect);
            this.setEs(max);
            this.setEf(max + du);
        }
    }


    public void addPre(ActItem actItem){
        if(this.pre == null){
            this.pre = new ArrayList<>();
        }
        pre.add(actItem);


    }

    public int getEs() {
        return es;
    }

    public void setEs(int es) {
        this.es = es;
    }

    public int getLs() {
        return ls;
    }

    public void setLs(int ls) {
        this.ls = ls;
    }

    public int getEf() {
        return ef;
    }

    public void setEf(int ef) {
        this.ef = ef;
    }

    public int getLf() {
        return lf;
    }

    public void setLf(int lf) {
        this.lf = lf;
    }

    public int getDu() {
        return du;
    }

    public void setDu(int du) {
        this.du = du;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<ActItem> getNext() {
        return next;
    }

    public void setNext(List<ActItem> next) {
        this.next = next;
    }

    public List<ActItem> getPre() {
        return pre;
    }

    public void setPre(List<ActItem> pre) {
        this.pre = pre;
    }


    @Override
    public String toString() {
        return "Item{" +
                "name='" + name + '\'' +
                ", rd=" + du +
                ", es=" + es +
                ", ef=" + ef +
                ", ls=" + ls +
                ", lf=" + lf +
                '}';
    }

    public static void main(String[] args) {
        ActItem a = new ActItem("a",2);
        ActItem b = new ActItem("b",3);
        ActItem c = new ActItem("c",4);
        ActItem d = new ActItem("d",2);
        ActItem e = new ActItem("e",5);
        ActItem f = new ActItem("f",6);

        a.addNext(b);a.addNext(c);
        b.addNext(d);
        c.addNext(e);

        d.addNext(f);
        e.addNext(f);


        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
        System.out.println(f);
    }

}
